//
//  SignAPI.swift
//  Health-Doctor
//
//  Created by 勇胡 on 2022/10/18.
//  Copyright © 2022 掌上卫健. All rights reserved.
//签名授权模块

import Foundation
import Moya

let signProvider = MoyaProvider<SignAction>(session:SignAction.custoSession(),plugins:NetworkLoggerPlugin.plugins())

enum SignAction {
    //获取签约居民列表
    case persionSignList(param:[String:Any])
    //保存个人签名
    case saveSign(param:[String:Any])
    //删除授权
    case deleteAuthSign(param:[String:Any])
    //保存用户业务授权(授权各个功能菜单)
    case saveAuthorized(param:[String:Any])
   //选择团队列表
    case selectTeamList(param:[String:Any])
    //获取团队详情
    case getTeamDetail(param:[String:Any])
    //查询指定用户当前的签约信息
    case getSignDetail(param:[String:Any])
    //根据签约记录ID查询签约信息
    case getSignDetailByContractId(param:[String:Any])
    //新增签约
    case addSing(param:[String:Any])
    //续约
    case reAddSign(param:[String:Any])
    //获取签约记录
    case getSignRecord(param:[String:Any])
    //解约
    case signCancel(param:[String:Any])
    //重新签约
    case signAgain(param:[String:Any])
    //获取签约协议里面的公章信息
    case getOfficialSeal(param:[String:Any])
    //申请签约记录
    case getAutoSignApplyRecord(param:[String:Any])
    //签约申请详情
    case getSignApplyDetail(param:[String:Any])
    //审核签约
    case checkApplySign(param:[String:Any])
    //获取自主签约待审核数量
    case getWaitCheckCount(param:[String:Any])
    //签约居民搜索
    case searchSignArchives(param:[String:Any])
    //云平台签约详情数据转本地签约详情数据
    case cloudSignToWoniuDetail(param:[String:Any])
}

extension SignAction: TargetType {
    var baseURL: URL {
        return URL(string: "https://wnjk.2woniu.cn")!
    }
    
    var path: String {
        
        var detailPath: String = ""
        switch self {
        case .saveSign:
            detailPath = "staff/profile/signature/save"
        case .deleteAuthSign:
            detailPath = "staff/profile/authorized/rm"
        case .saveAuthorized:
            detailPath = "staff/profile/authorized/save"
        case .persionSignList:
            detailPath = "cloudauth/sign/persionSign/list"
        case .selectTeamList:
            detailPath = "cloudauth/team/getPersionTeamList"
        case .getTeamDetail:
            detailPath = "cloudauth/team/getInfo"
        case .getSignDetail:
            detailPath = "cloudauth/sign/getDetail"
        case .addSing:
            detailPath = "cloudauth/sign/add"
        case .reAddSign:
            detailPath = "cloudauth/sign/renew"
        case .getSignRecord:
            detailPath = "cloudauth/sign/signRecord"
        case .signCancel:
            detailPath = "cloudauth/sign/cancel"
        case .signAgain:
            detailPath = "cloudauth/sign/reAdd"
        case .getSignDetailByContractId:
            detailPath = "cloudauth/sign/getDetailByContractId"
        case .getOfficialSeal:
            detailPath = "staff/profile/officialSeal"
        case .getAutoSignApplyRecord:
            detailPath = "cloudauth/iSign/applySignRecord"
        case .getSignApplyDetail:
            detailPath = "cloudauth/iSign/applySignDetail"
        case .checkApplySign:
            detailPath = "cloudauth/iSign/signAudit"
        case .getWaitCheckCount:
            detailPath = "cloudauth/iSign/existsUnaudited"
        case .searchSignArchives:
            detailPath = "cloudauth/resident/archives/keyword/idcard"
        case .cloudSignToWoniuDetail:
            detailPath = "cloudauth/sign/convert"
        }
        return detailPath
    }
    
    var params: [String:Any] {
        var params:[String:Any] = [:]
        switch self {
        case .saveSign(let param),
            .deleteAuthSign(let param),
            .saveAuthorized(let param),
            .persionSignList(let param),
            .selectTeamList(let param),
            .getTeamDetail(let param),
            .getSignDetail(let param),
            .addSing(let param),
            .reAddSign(let param),
            .getSignRecord(let param),
            .signCancel(let param),
            .signAgain(let param),
            .getSignDetailByContractId(let param),
            .getOfficialSeal(let param),
            .getAutoSignApplyRecord(let param),
            .getSignApplyDetail(let param),
            .checkApplySign(let param),
            .getWaitCheckCount(let param),
            .searchSignArchives(let param),
            .cloudSignToWoniuDetail(let param):
            for (k,v) in param {
                params[k] = v
            }
        }
        return params
        
    }
    
    var headers: [String : String]? {
        return getHttpHeader(token: "")
    }
    
    var method: Moya.Method {
        switch self {
        case .selectTeamList,.getTeamDetail,.getSignDetail,.getSignDetailByContractId,.getOfficialSeal,.getAutoSignApplyRecord,.getSignApplyDetail,.getWaitCheckCount,.searchSignArchives:
            return .get
        default:
            return .post
        }
    }
    
    var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        if self.method == .get {
            return.requestParameters(parameters: self.params, encoding:GetEncoding())
        }
        switch self {

        case .persionSignList,.getSignRecord,.addSing,.signCancel,.signAgain,.reAddSign,.getSignDetailByContractId,.checkApplySign,.cloudSignToWoniuDetail:
            return .requestParameters(parameters: self.params, encoding:JSONEncoding())
        default:
            return .requestParameters(parameters:self.params, encoding: URLEncoding.default)
        }
    }
}
